package com.jxb.jingchen.buletooth;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * @author jinxb
 * date     : 2021/8/19
 * desc     :
 */
public class BluetoothReceiver extends BroadcastReceiver {

    private final OnDeviceFoundListener onDeviceFoundListener;

    public BluetoothReceiver(OnDeviceFoundListener onDeviceFoundListener) {
        this.onDeviceFoundListener = onDeviceFoundListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // 发现设备
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // 从Intent中获取蓝牙设备
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            onDeviceFoundListener.onDeviceFound(device);
        }
    }

    public IntentFilter getFilter(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        return filter;
    }

    public interface OnDeviceFoundListener {
        void onDeviceFound(BluetoothDevice deviceBean);
    }
}
